declare let JIM: any;
module MC.Call {

    /** 极光的用户名 */
    let uid: string;
    let pass: string;
    let LoadFun: Function = null;
    let AudioQueue:MC.AudioModel.Audio = null;//new MC.AudioModel.Audio;
    export function Jiguang(_fun: Function) {
        console.log('弃用');return;/*
        AudioQueue=new MC.AudioModel.Audio;
        MC.loadJsUrl('/h5/js/jiguang.js', () => { 
            LoadFun = _fun;
            window.JIM = new JMessage({
                debug: false//true false
            });
            MC.api('getjiguang', {}, (data: any) => {
                uid = data[4]
                ///  MC.user.getInfo(this.uid);
                pass = data[5];
                JiguangInit(data[0], data[1], data[2], data[3]);
                ///	console.log(data);
            });
        });
         AudioQueue.addFile("msg", "/media/msg.mp3");
          AudioQueue.load("",()=>{
			});*/
    }

    function loadAudio(){
        if(AudioQueue){
            return;
        }
        AudioQueue=new MC.AudioModel.Audio;
        AudioQueue.addFile("msg", "/media/msg.mp3");
        AudioQueue.load("",()=>{
            MC.dispatchEvent("loadAudio");
        });
    }

    export function newmsgAudio() {
        loadAudio();
        MC.on("loadAudio",()=>{
            AudioQueue.Play('msg');
        });
    }
    export  function JiguangInit(appkey: string, random_str: string, timestamp: string, signature: string,_uid:string,pass:string) {
        window.JIM = new JMessage({
            debug: false//true false
        });
        uid=_uid;
        JIM.init({
            "appkey": appkey,
            "random_str": random_str,
            "signature": signature,
            "timestamp": timestamp,
            "flag": 1
        }).onSuccess(function (data: any) {
          //  console.log('success:', uid,data);
            JIM.login({
                'username': uid,
                'password': pass
            }).onSuccess(function (data: any) {
              ///  LoadFun && LoadFun(uid, data);
                Success(data);
            }).onFail(function(data:any){
                console.log(data);
                MC.msg.alert(data.message);
                //同上
            });;
        }).onFail(function (data: any) {
            console.log('error:', data);
            MC.msg.alert(data.message);
        });
    }

    /**
     *绑定初始化成功的事件
     * */
    export function onSuccess(fun:Function) {
        MC.on('jiguangonSuccess',fun);
    }


    /**
     * 极光加载成功调用
     * */
    function Success( data:any) {
     //   console.log(data);
        MC.dispatchEvent('jiguangonSuccess');
        /*
        JIM.onMsgReceive(function (data: any){
            onMsgReceive(data);
        });*/
        $("body").on("click","[callid]",function(){
           // console.log(eve);
            let target=$(this);
            let jiguangid=target.attr('callid');
            window.open("/h5/jiguang/win.html?callid="+jiguangid,"_self");
        });
    }
    
    
    /**
     * 绑定聊天消息实时监听
     * */
    export  function onMsgReceive(fun:Function) {
        JIM.onMsgReceive(function (data: any){
            fun(data);
        });
    }
    
    /**
     * 获取会话列表
     * */
    export function getConversation(fun:any) {
        JIM.getConversation().onSuccess((data: any) => {
            fun(data);
        });
    }

    /***
     * 判断是否为平台管理用户
    */
    export function isAdmin(id:string){
        if(id=='mc_1'){
            return true;
        }else{
            return false;
        } 
    }
        

}